export class CLonLatTrans {
  constructor(p_fdLongitude, p_fdLatitude, p_fdRotateAngle) {
    this.m_fdLonggitude0 = p_fdLongitude // 基准点经度
    this.m_fdLatitude0 = p_fdLatitude // 基准点纬度
    this.m_fdRotateAngle = p_fdRotateAngle // 基准点北向夹角
    this.m_fdMathPi = Math.PI // PI
    this.m_fdLongRa = 6378137.0 // 地球半径长轴
    this.m_fdShortRb = 6356752.3142 // 地球半径短轴
    this.m_fdCosA = Math.cos((p_fdRotateAngle * this.m_fdMathPi) / 180.0)
    this.m_fdSinA = Math.sin((p_fdRotateAngle * this.m_fdMathPi) / 180.0)
  }

  lonlat2xy(lonlat) {
    const p_fdLongitude = lonlat[0]
    const p_fdLatitude = lonlat[1]
    const l_fdX = (((p_fdLongitude - this.m_fdLonggitude0) * this.m_fdMathPi) / 180.0) * this.m_fdLongRa * Math.cos((this.m_fdLatitude0 * this.m_fdMathPi) / 180.0)
    const l_fdY = (((p_fdLatitude - this.m_fdLatitude0) * this.m_fdMathPi) / 180.0) * this.m_fdShortRb
    let cosA = this.m_fdCosA
    let sinA = this.m_fdSinA
    if (this.m_fdRotateAngle !== 0) {
      cosA = Math.cos((this.m_fdRotateAngle * this.m_fdMathPi) / 180.0)
      sinA = Math.sin((this.m_fdRotateAngle * this.m_fdMathPi) / 180.0)
    }
    // 根据注释中的代码逻辑，这里使用了不同的公式
    // return [l_fdX * cosA + l_fdY * sinA, l_fdY * cosA - l_fdX * sinA];
    return [l_fdX * cosA - l_fdY * sinA, l_fdY * cosA + l_fdX * sinA]
  }

  xy2lonlat(p_fdX, p_fdY, p_fdRotateAngle = 0) {
    let cosA = this.m_fdCosA
    let sinA = this.m_fdSinA

    if (p_fdRotateAngle !== 0) {
      cosA = Math.cos((p_fdRotateAngle * this.m_fdMathPi) / 180.0)
      sinA = Math.sin((p_fdRotateAngle * this.m_fdMathPi) / 180.0)
    }

    // 根据注释中的代码逻辑，这里使用了不同的公式
    // let l_fdX = p_fdX * cosA - p_fdY * sinA;
    // let l_fdY = p_fdY * cosA + p_fdX * sinA;
    const l_fdX = p_fdX * cosA + p_fdY * sinA
    const l_fdY = p_fdY * cosA - p_fdX * sinA

    const p_fdLongitude = ((l_fdX / (this.m_fdLongRa * Math.cos((this.m_fdLatitude0 * this.m_fdMathPi) / 180.0))) * 180.0) / this.m_fdMathPi + this.m_fdLonggitude0
    const p_fdLatitude = l_fdY / ((this.m_fdMathPi / 180.0) * this.m_fdShortRb) + this.m_fdLatitude0
    return [p_fdLongitude, p_fdLatitude]
  }

  getAngle() {
    return this.m_fdRotateAngle
  }
}
